"use strict";

let Router = require('koa-router');
let config = require('../../../config/config.js');
let daoDispatcher = require('../../../dao/book.js');
let debug = require('debug')('book');

debug('router:' + config.baseUrl + '/v2/book');
let router = new Router({
    prefix: config.baseUrl + '/v2/book'
});

router.get('/test', function*() {
    this.body = this.url + ' test work fine';
});

/**
 * add a book
 * post /api/v2/book
 {
     "name": "tale",
     "author": "kate",
     "price": 13.99
 }
 */
router.post('/', function*() {
    let result = yield daoDispatcher.addOne(this.request.inParams);
    this.body = result;
});

/**
 * get /api/v2/book/query?sql=select * from books where name='tale'
 */
router.get('/query', function*() {
    let sql = this.request.inParams.sql;
    sql = decodeURIComponent(sql);
    debug(sql);
    let result = yield daoDispatcher.rawQuery(sql);
    this.body = result;
});
module.exports = router.routes();


router.get('/:id', function*() {
    let result = yield daoDispatcher.findById(this.params.id);
    this.body = result;
});